
//  Created by 莫瑞权 on 2022/5/7.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFChatPublicBroadcastingCell: UITableViewCell {

    @objc public var viewWidth:CGFloat = 0
    @objc public var model:TF1to16LiveChatData?
    {
        didSet{
            if (model != nil)
            {
                self.modelUI(model!)
            }
            
        }
        
    }

    let background = UIView()
    let publicBroadcastingButton = UIButton()
    let name = UILabel()
    let time = UILabel()
    let content = TF_StoyCopyTextView()
 
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.addSubview(self.background)
        self.background.backgroundColor = UIColor(red:39/255.0 , green: 43/255.0, blue: 49/255.0, alpha: 1)
  
        self.publicBroadcastingButton.backgroundColor = UIColor.clear
        self.background.addSubview(self.publicBroadcastingButton)
        self.publicBroadcastingButton.setImage(UIImage.init(named: "HTLive.bundle/publicBroadcasting"), for: .normal)
        self.publicBroadcastingButton.setTitleColor(UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1), for: .normal)
        
        //其它人的名字
        self.name.backgroundColor = UIColor.clear
        self.name.numberOfLines = 0
        self.background.addSubview(self.name)
        self.name.isHidden = true
        
        //时间
        self.time.backgroundColor = UIColor.clear
        self.time.numberOfLines = 0
        self.background.addSubview(self.time)
        self.time.isHidden = true
 
        self.content.backgroundColor = UIColor.clear
        self.content.isHidden = true;
        self.content.textContainer.lineFragmentPadding = 0
        self.content.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.content.font = UIFont.PingFang(size: 14)
        self.content.isScrollEnabled = false
        self.content.isEditable = false
        self.background.addSubview(self.content)
        
      
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.background.layer.cornerRadius = 10
        self.background.frame = CGRect(x: 14.5, y: 12.5, width: self.frame.size.width-14.5-14.5, height: self.frame.size.height-12.5)
       self.publicBroadcastingButton.frame = CGRect(x: 11, y: 10, width: 16, height: 16)
     
    
        let contentY:CGFloat = 13+20+3
        let contentH:CGFloat = self.frame.size.height-contentY-13;//13是下边距
        self.content.frame = CGRect(x:11 , y: contentY, width: self.frame.size.width - 26.5 - 26.5, height: contentH)

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func modelUI(_ model:TF1to16LiveChatData)
    {
        //奖励
        self.name.isHidden = false
        self.content.isHidden = false
        self.time.isHidden = false
        //其它人的名字
        
        var nickname = "广播"

        if (model.isAnnouncement) {

            nickname = "公告"
            //通知
        }else if(model.isAnnounceRoll){
            
            nickname = "tongzhi"
        }

        
        self.name.attributedText = NSMutableAttributedString.getAttributed(string: nickname, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 182/255.0, blue: 55/255.0, alpha: 1))


        let nameH:CGFloat = 20
        let nameY:CGFloat = 8
        let size: CGSize = nickname.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
        self.name.frame = CGRect(x: 36, y: nameY, width: size.width, height: nameH)
        
 
        let timeStr =  "  " + (TalkfunSharedTools.getTimestamp(mStr: model.time as NSString) as String)
                
        let othersNameString =    NSMutableAttributedString.getAttributed(string: timeStr, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1))
          
            
        self.time.attributedText = othersNameString
        
        let size1: CGSize = timeStr.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
               
        
        self.time.frame = CGRect(x: self.name.frame.maxX+10, y: nameY, width: size1.width, height: nameH)
        
        let AttributedString = NSMutableAttributedString.getAttributed(string: model.msg , font: UIFont.PingFang(size: 14), foregroundColor: UIColor.white)

        self.content.addHyperlink(font: UIFont.PingFang(size: 14), attributedText: AttributedString)
            self.content.attributedText = AttributedString
   
        
        let size11 = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth: self.viewWidth-14-14, labelfont: UIFont.PingFang(size: 14))
        
        
            
        self.content.frame = CGRect(x: 0, y: 77, width: self.viewWidth-14-14, height: size11.height)
        
        self.name.textAlignment = .left
        self.content.textAlignment = .left
    }
    
}
